unit DataSetUtils;

interface

uses SysUtils, Db, DataDic;

procedure CopyFields(Src, Dest: TDataSet);
procedure CopyData(Src, Dest: TDataSet);
function LookForValue(DS: TDataSet; KeyField: string; KeyValue: variant): boolean;

implementation

function LookForValue(DS: TDataSet; KeyField: string; KeyValue: variant): boolean;
begin
  result := False;
  Ds.Next;
  while not DS.Eof do begin
    if DS[KeyField] = KeyValue then begin
      result := True;
      Break;
    end
    else
      DS.Next;
  end;
end;

procedure CopyFields(Src, Dest: TDataSet);
var
  i: integer;
  src_fld, fld: TField;
  FldClass: TFieldClass;
begin
  for i := 0 to Src.Fields.Count - 1 do begin
    src_fld := Src.Fields[i];
    FldClass := DefaultFieldClasses[src_fld.DataType];
    fld := FldClass.Create(Dest.Owner);
    fld.FieldKind := src_fld.FieldKind;
    if fld.FieldKind = fkLookUp then begin
      Fld.LookupDataset := src_fld.LookupDataset;
      Fld.KeyFields := src_fld.KeyFields;
      Fld.LookupKeyFields := src_fld.LookupKeyFields;
      Fld.LookupResultField := src_fld.LookupResultField;
    end;
    fld.FieldName := src_fld.FieldName;
    fld.Size := src_fld.Size;
    fld.Index := src_fld.Index;
    fld.DisplayLabel := src_fld.DisplayLabel;
    fld.DisplayWidth := src_fld.DisplayWidth;
    if fld is TBCDField then
      TBCDField(fld).DisplayFormat := TBCDField(src_fld).DisplayFormat;
    fld.Visible := src_fld.Visible;
    fld.DataSet := Dest;
  end;
  Dest.OnCalcFields := Src.OnCalcFields;
end;

procedure CopyData(Src, Dest: TDataSet);

  procedure CopyRec;
  var i: integer;
  begin
    Dest.Append;
    for i := 0 to Src.Fields.Count - 1 do
      if Src.Fields[i].FieldKind = fkData then
        Dest.Fields[i].Value := Src.Fields[i].Value;
    Dest.Post;
  end;

begin
  Src.First;
  while not Src.Eof do begin
    CopyRec;
    Src.Next;
  end;
end;


end.
